package offer;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;

public class offerII_0084 {
    public List<List<Integer>> permuteUnique(int[] nums) {
        List<List<Integer>> result=new LinkedList<>();
        List<Integer> path=new LinkedList<>();
        HashSet<Integer> set=new HashSet<>();
        search(result,path,set,nums,0);
        return result;
    }
    public void search(List<List<Integer>> result,List<Integer> path,HashSet<Integer> set,int[] nums,int num){
        if(num==nums.length){
            result.add(new ArrayList<>(path));
            return;
        }
        HashSet<Integer> tmp=new HashSet<>();
        for(int i=0;i<nums.length;++i){
            if(!tmp.contains(nums[i])&&!set.contains(i)){
                tmp.add(nums[i]);
                set.add(i);
                path.add(nums[i]);
                search(result, path, set, nums, num+1);
                set.remove(i);
                path.remove(path.size()-1);
            }
        }
    }
}
